package mysqlInit

import (
	"0507zk/common/initialize"
	"gorm.io/gorm"
)

type User struct {
	gorm.Model
	Mobile   string `gorm:"type:char(11);not null;index:ip_idx"`
	Name     string `gorm:"type:varchar(10);not null;index:ip_idx"`
	Password string `gorm:"type:varchar(32);not null;index:ip_idx"`
}

func (u User) GetUserMobile(mobile string) (user *User, err error) {
	err = initialize.DB.Where("mobile=?", mobile).First(&user).Error
	if err != nil {
		return nil, err
	}
	return
}

type Product struct {
	gorm.Model
	Name   string  `gorm:"type:varchar(11);not null;index:ip_idx"`
	Price  float64 `gorm:"type:decimal(10,2);not null;index:ip_idx"`
	Stock  int     `gorm:"type:int(10);not null;index:ip_idx"`
	Status int     `gorm:"type:int(10);not null;index:ip_idx"`
}

func (p Product) GetProductName(name string) (product []*Product, err error) {
	err = initialize.DB.Where("name=?", name).First(&product).Error
	if err != nil {
		return nil, err
	}
	return
}

func (p Product) GetProduct() (product []*Product, err error) {
	err = initialize.DB.Find(&product).Error
	if err != nil {
		return nil, err
	}
	return
}
